#include "page_manager.h"
#include <stdio.h>
#include "epd.h"
#include "esp_partition.h"
#include "esp_log.h"
#include <string.h>


typedef struct{
	uint8_t select_id;
	uint16_t x;
	uint16_t y;
	const uint8_t *icon_array;
	uint8_t icon_size;
	uint16_t t_x;
	uint16_t t_y;
	char *icon_title;
}menu_item_t;

#define ICON_SIZE 96
#define FONT_SIZE 24
#define FONT_2_OFFSET FONT_SIZE
#define FONT_3_OFFSET (FONT_SIZE+FONT_SIZE/2)

static menu_item_t menu_item[8] = {
	{0,  120-ICON_SIZE/2-1+10, 160-ICON_SIZE/2-1, gImage_read_96, ICON_SIZE, 		120-FONT_2_OFFSET+1+10, 160-ICON_SIZE/2-1+ICON_SIZE+5 ,"\xD4\xC4\xB6\xC1"},
	{1,  360-ICON_SIZE/2-1-10, 160-ICON_SIZE/2-1, gImage_alarmclock_96, ICON_SIZE,  360-FONT_2_OFFSET-1-10, 160-ICON_SIZE/2-1+ICON_SIZE+5 , "\xC4\xD6\xD6\xD3"},
	{2,  120-ICON_SIZE/2-1+10, 320-ICON_SIZE/2-1, gImage_pomodoro_96, ICON_SIZE,	120-FONT_3_OFFSET-1+10, 320-ICON_SIZE/2-1+ICON_SIZE+5, "\xB7\xAC\xC7\xD1\xD6\xD3"},
	{3,  360-ICON_SIZE/2-1-10, 320-ICON_SIZE/2-1, gImage_schedule_96, ICON_SIZE,	360-FONT_2_OFFSET-1-10, 320-ICON_SIZE/2-1+ICON_SIZE+5, "\xBC\xC6\xBB\xAE"},
	{4,  120-ICON_SIZE/2-1+10, 480-ICON_SIZE/2-1, gImage_words_96, ICON_SIZE,		120-FONT_3_OFFSET-1+10, 480-ICON_SIZE/2-1+ICON_SIZE+5, "\xBC\xC7\xB5\xA5\xB4\xCA"},
	{5,  360-ICON_SIZE/2-1-10, 480-ICON_SIZE/2-1, gImage_weather_96, ICON_SIZE,		360-FONT_2_OFFSET-1-10, 480-ICON_SIZE/2-1+ICON_SIZE+5, "\xCC\xEC\xC6\xF8"},
	{6,  120-ICON_SIZE/2-1+10, 640-ICON_SIZE/2-1, gImage_picture_96, ICON_SIZE,		120-FONT_2_OFFSET-1+10, 640-ICON_SIZE/2-1+ICON_SIZE+5, "\xCD\xBC\xC6\xAC"},
	{7,  360-ICON_SIZE/2-1-10, 640-ICON_SIZE/2-1, gImage_setting_96, ICON_SIZE,		360-FONT_2_OFFSET-1-10, 640-ICON_SIZE/2-1+ICON_SIZE+5, "\xC9\xE8\xD6\xC3"}
};

uint8_t last_menu_select_id = 0;
uint8_t menu_select_id = 0;
uint8_t menu_update_flag = 1;




static void Setup(uint8_t condition)
{
	uint8_t i;

	last_menu_select_id = 0;
	menu_select_id = 0;
	menu_update_flag = 0;
	
	printf("page menu setup\n");
#if 1
	Paint_Clear(WHITE);
	// 绘制图标
	for(i = 0; i < 8; i++)
	{
		EPD_Paint_DrawPartIcon(menu_item[i].icon_array, menu_item[i].x, menu_item[i].y, menu_item[i].icon_size, menu_item[i].icon_size);
		SSD1677_DispString_EN_CH(menu_item[i].t_x, menu_item[i].t_y, BLACK, FONT_SIZE, SongTi, 0,  menu_item[i].icon_title);
		if(i == menu_select_id)
			Paint_DrawRectangle(menu_item[i].x - 2, menu_item[i].y - 2, menu_item[i].x + menu_item[i].icon_size + 2, menu_item[i].y + menu_item[i].icon_size + 5 + FONT_SIZE + 2, BLACK, DRAW_FILL_EMPTY, DOT_PIXEL_2X2);
	}
    EPD_Paint_Update();
#endif
#if 0 
	for(i = 0; i < 8; i++)
	{
		epd_gd_dis_part_ram(menu_item[i].x, menu_item[i].y,menu_item[i].icon_array, menu_item[i].icon_size, menu_item[i].icon_size);
	}
	epd_gd_part_update();
#endif


	epd_delay(100);
} 


static void Loop(void)
{
	uint8_t i = 0;
	if(menu_update_flag)
	{
		printf("page_menu Loop\n");
		printf("page_menu select_id: %d\n", menu_select_id);
		menu_update_flag = 0;
		Paint_DrawRectangle(menu_item[last_menu_select_id].x - 2, menu_item[last_menu_select_id].y - 2, menu_item[last_menu_select_id].x + menu_item[last_menu_select_id].icon_size + 2, menu_item[last_menu_select_id].y + menu_item[last_menu_select_id].icon_size + 5 + FONT_SIZE + 2, WHITE, DRAW_FILL_EMPTY, DOT_PIXEL_2X2);
		Paint_DrawRectangle(menu_item[menu_select_id].x - 2, menu_item[menu_select_id].y - 2, menu_item[menu_select_id].x + menu_item[menu_select_id].icon_size + 2, menu_item[menu_select_id].y + menu_item[menu_select_id].icon_size + 5 + FONT_SIZE + 2, BLACK, DRAW_FILL_EMPTY, DOT_PIXEL_2X2);
		printf("draw over\n");
    	EPD_Paint_Update_A2();
		printf("update over\n");
#if 0
		for(i = 0; i < 8; i++)
		{
			if(menu_select_id == i)
				epd_1gray_display_part_icon_edge(menu_item[i].icon_array, menu_item[i].x, menu_item[i].y, menu_item[i].icon_size, menu_item[i].icon_size);
			else
				epd_1gray_display_part_icon(menu_item[i].icon_array, menu_item[i].x, menu_item[i].y, menu_item[i].icon_size, menu_item[i].icon_size);
		}
		epd_auto_refresh(1);
#endif

	}
} 

static void Exit(uint8_t condition)
{
	printf("page_menu Exit\n");
	if(condition == PAGE_CONDITION_RETURN)
	{
		epd_1gray_init();
	}
	else 
	{
		
	}


}

static void Event(uint8_t event)
{
	printf("page_menu Event: %d\n", event);
	if(event == 4) // 退出
	{
		page_manager_switch_page(page_id_home, PAGE_CONDITION_RETURN);
	}
	else if(event == 2) // 下
	{
		last_menu_select_id = menu_select_id;
		menu_select_id++;
		if(menu_select_id > 7)
		{
			menu_select_id = 0;
		}
		menu_update_flag = 1;

	}
	else if(event == 3) // 上
	{
		last_menu_select_id = menu_select_id;
		if(menu_select_id == 0)
		{
			menu_select_id = 7;
		}
		else
		{
			menu_select_id--;
		}
		menu_update_flag = 1;
	}
	else if(event == 1) // 进入
	{
		if(menu_select_id == 0)
		{
			page_manager_switch_page(page_id_read, PAGE_CONDITION_ENTER);
		}
	}

}




// 注册页面
void page_register_page_id_menu(uint8_t page_id)
{
	page_register(page_id, NULL, NULL, Setup, Loop, Exit, Event);
}




